package com.feng.service.Impl;

import com.feng.dao.CarDao;
import com.feng.dao.GoodDao;
import com.feng.dao.OrderDao;
import com.feng.dao.StoreDao;
import com.feng.dto.Result;
import com.feng.pojo.*;
import com.feng.service.CarService;
import com.feng.service.OrderService;
import com.feng.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    int todayTotalOrder;
    @Autowired
    OrderDao orderDao;
    @Autowired
    RedisService redisService;

    @Override
    public Result create(List<Good> goods, Integer uid) {
        long currentTime = System.currentTimeMillis();
        List<Car> cars = (List<Car>) redisService.get("car" + uid);
        HashMap<Integer, List<Good>> sortGood = new HashMap<>();
        List<Integer> storeIds = new ArrayList<>();
        //通过商店分类
        for (Good good : goods) {
            List<Good> tempGoods;
            if (sortGood.containsKey(good.getStoreId())) {
                tempGoods = sortGood.get(good.getStoreId());
            } else {
                tempGoods = new ArrayList<>();
                //记录所有storeId
                storeIds.add(good.getStoreId());
            }
            tempGoods.add(good);
            sortGood.put(good.getStoreId(), tempGoods);
        }
        //循环所有商品
        for (Integer storeId : storeIds) {
            String orderNumber = currentTime + String.format("%04d", storeId) + String.format("%05d", todayTotalOrder);
            for (Good good : sortGood.get(storeId)) {
                Car tempCar = cars.stream()
                        .filter(car -> car.getGoodId().equals(good.getId()))
                        .findFirst()
                        .orElse(null);
                Order order = new Order(null, good.getId(), uid, "未付款", orderNumber, tempCar.getNumber());
                orderDao.create(order);
                //删除掉已经下订单的购物车
                cars.remove(tempCar);
                redisService.set("car" + uid, cars);
            }
            todayTotalOrder++;
        }
        return Result.builder().success(true).massage("插入成功").build();
    }

    @Override
    public Result create(Integer goodId, Integer storeId, Integer goodNum, Integer uid) {
        long currentTime = System.currentTimeMillis();
        String orderNumber = currentTime + String.format("%04d", storeId) + String.format("%05d", todayTotalOrder);
        Order order = new Order(null, goodId, uid, "未付款", orderNumber, goodNum);
        orderDao.create(order);
        todayTotalOrder++;
        return Result.builder().success(true).massage("插入成功").build();
    }

    @Override
    public Result pay(List<Integer> orderIds) {
        String status = "已付款";
        orderDao.statusUpdate(orderIds,status);
        return Result.builder().success(true).massage("付款成功").build();
    }
}